package games.checkers;

import games.checkers.chipPath.pathNode;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;

public class chip extends Ellipse2D.Float
{
	private chipManager manager;
	private checkersBoard board;
	private chipBin bin;
	private Color color;
	private GradientPaint highlight;
	private int tile;
	private byte player;
	private boolean king;
	
	private boolean onBoard;
	
	public chip(int t, int d, Color c,byte p, checkersBoard cb, chipManager m, chipBin b)
	{
		board = cb;
		player = p;
		manager = m;
		bin = b;
		onBoard = true;
		
		color = c;
		setDiameter(d);
		setTile(t);
		
		king = false;
	}
	
	public void kingMe()
	{
		king = true;
	}
	
	public boolean isKing()
	{
		return king;
	}
	
	public void jumpMe()
	{
		onBoard = false;
		tile = -1;
		setDiameter(42);
		moveTo(bin.binSpot());
	}
	
	public int getTile()
	{
		return tile;
	}
	
	public void setTile(int t)
	{
		tile = t;
		Point p = board.getTilePosition(tile);
		p.x += width/2+10;
		p.y += height/2+10;
		moveTo(p);
		if ((player == chipManager.player1) && (tile < 4)) kingMe();
		if ((player == chipManager.player2) && (tile > 27)) kingMe();
	}
	
	public void setColor(Color c)
	{
		color = c;
	}
	
	public void setDiameter(int d)
	{
		width = d;
		height = d;
	}
	
	public void onBoard(boolean val)
	{
		onBoard = val;
	}
	
	public boolean onBoard()
	{
		return onBoard;
	}
	
	public void moveTo(Point p)
	{
		x = p.x-width/2-5;
		y = p.y-height/2-5;
		highlight = new GradientPaint(x,y,new Color(255,255,255),x+width,y+height,new Color(0,0,0));
	}
	
	public int player()
	{
		return player;
	}
	
	public ArrayList<ArrayList<pathNode>> validPaths()
	{
		ArrayList<ArrayList<pathNode>> paths;
		
		chipPath p = new chipPath(tile,manager.boardArray(),board, manager);
		paths = p.getAllPaths();
		
		return paths;
	}
	
	public void paint(Graphics g)
	{
		Graphics2D g2d = (Graphics2D) g;
		Color oldColor = g.getColor();
	
		//draw the bottom
		g.setColor(color.darker()); //botColor);
		if (tile > -1) g2d.fill(new Ellipse2D.Float(x+5, y+5, width, height));
		else g2d.fill(new Ellipse2D.Float(x+2, y+2, width, height));
		//draw the top
		g2d.setStroke(new BasicStroke(1));
		g.setColor(color);
		g2d.fill(this);
		
		//draw the highlight
		g2d.setPaint(highlight);
		g2d.draw(this);
		
		// if a king and on the board, show it
		if (king && onBoard)
		{
			g2d.setColor(new Color(255,215,0));
			g2d.fill(new Ellipse2D.Float(x+width/3, y+height/3, width/3, height/3));
		}
		
		//reset old color
		g.setColor(oldColor);
	}
	
}
